package testClasses.misc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme3.light.DirectionalLight;
import com.jme3.light.PointLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh.Mode;
import com.jme3.scene.control.LightControl;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;

import controller.AbstractController;

import testClasses.ATest;
import testClasses.TestingSystem;
import utilities.CoordConvert;
import utilities.CustomMesh;
import utilities.HexTileGen;
import utilities.Key;

public class HexGridCreationTest extends ATest {

	private static final Logger logger = Logger.getLogger(HexGridCreationTest.class.getName());
	
	public HexGridCreationTest(TestingSystem system) {
		super(system);
		name = "HexGridCreationTest";
		logger.log(Level.INFO, "HexGridCreationTest: Created");
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean execute() {
		logger.log(Level.INFO, "HexGridCreationTest: execute()");
		
		//HashMap<Key, AbstractController> controllerMap = new HashMap<Key, AbstractController>();
		//events.setControllerMapReference(controllerMap);
		
		int radius = 1;
		
		ArrayList<ArrayList<Integer>> hexTileLocations = HexTileGen.genTileLocations(radius);
		logger.log(Level.INFO, "Done loading locations");
		//http://en.wikipedia.org/wiki/Centered_hexagonal_number
		int numberHexes = 3*radius*(radius-1)+1;
		int vertsPerHex = 12; //6 lines * 2 verts per line
		CustomMesh lineMesh = new CustomMesh(numberHexes*vertsPerHex);
		lineMesh.setMode(Mode.Lines);
		
		float sideLength = (float) (1/Math.sqrt(3));
		float xVal = 0.5f;
		float yVal = sideLength;
		
		float tempX = 0.5f;
		float tempY = sideLength;
		
		float centerX;
		float centerY;
		
		//for (int i = 0; i < 0; i++) {
		for (Integer i = 0; i < hexTileLocations.size(); i++) {
			ArrayList<Double> coords = CoordConvert.hex2Cart(hexTileLocations.get(i));
			//hexTileCartCoords.add(CoordConvert.hex2Cart(hexTileLocations.get(i)));
			
			centerX = coords.get(0).floatValue();
			centerY = coords.get(1).floatValue();
			
//			System.out.println("Coords: x= " + coords.get(0) + " y= " + coords.get(1));
//			System.out.println("TempX= " + tempX + " TempY= " + tempY);
//			//xVal = centerX + tempX;
//			//yVal = centerY + tempY;
//			System.out.println("xVal= "+ xVal + " yVal= " + yVal);
			
			
			
			lineMesh.addVertex(centerX-xVal,centerY-yVal/2,0);
			lineMesh.addVertex(centerX,centerY-yVal,0);
			
			lineMesh.addVertex(centerX,centerY-yVal,0);//			
			lineMesh.addVertex(centerX+xVal,centerY-yVal/2,0);
			
			lineMesh.addVertex(centerX+xVal,centerY-yVal/2,0);//
			lineMesh.addVertex(centerX+xVal,centerY+yVal/2,0);
			
			lineMesh.addVertex(centerX+xVal,centerY+yVal/2,0);//
			lineMesh.addVertex(centerX,centerY+yVal,0);
			
			lineMesh.addVertex(centerX,centerY+yVal,0);//
			lineMesh.addVertex(centerX-xVal,centerY+yVal/2,0);
			
			lineMesh.addVertex(centerX-xVal,centerY+yVal/2,0);//
			lineMesh.addVertex(centerX-xVal,centerY-yVal/2,0);//
		}
		lineMesh.setLineWidth(3.0f);
		lineMesh.finish();
		lineMesh.setStatic();
		
		//String s = i.toString();
		Geometry geo = new Geometry("test", lineMesh);
		
		Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
		mat.setColor("Color", ColorRGBA.Blue);
		mat.setColor("GlowColor", ColorRGBA.Blue);
		
//		Material mat = new Material(assets, "Common/MatDefs/Light/Lighting.j3md");
//		mat.setColor("Specular", ColorRGBA.Black);
//		mat.setFloat("Shininess", 50f);
		
		geo.setMaterial(mat);
		//geo.setLocalTranslation(coords.get(0).floatValue(), coords.get(1).floatValue(), 0);
		node.attachChild(geo);
		
//		Box box = new Box(1f,1f,1f);
//		Material mat = new Material(assets, "Common/MatDefs/Misc/Unshaded.j3md");
//		mat.setColor("Color", ColorRGBA.Green);
//		Geometry geo = new Geometry("test", box);
//		geo.setMaterial(mat);
//		geo.setLocalTranslation(0,0,0);
//		node.attachChild(geo);
		
//		DirectionalLight light = new DirectionalLight();
//		light.setColor(ColorRGBA.White);
//		light.setDirection(new Vector3f(0, 0, -1f).normalizeLocal());
		
//		Sphere sphere = new Sphere(20,20,0.2f);
//		Geometry geo = new Geometry("Test", sphere);
//		Material mat = new Material(assets, "Common/MatDefs/Misc/Unshaded.j3md");
//		mat.setColor("Color", ColorRGBA.Red);
//		mat.setColor("GlowColor", ColorRGBA.Red);
//		geo.setMaterial(mat);
//		geo.setLocalTranslation(0, 0, 0.2f);
//		
//		PointLight light = new PointLight();
//		light.setColor(ColorRGBA.Red);
//		light.setRadius(1.5f);
//		light.setPosition(new Vector3f(0,0,1f));
//		
//		node.attachChild(geo);
//		node.addLight(light);
//		LightControl lightControl = new LightControl(light);
//		geo.addControl(lightControl);
		
		System.out.println(hexTileLocations.size());
		
		
		
		
		
		
		
		return false;
	}

}
